/* Copyright 2015 David Overeem (dovereem@cox.net)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.xtce.apps.editor.ui;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import org.omg.space.xtce.AliasSetType;
import org.omg.space.xtce.AliasSetType.Alias;
import org.xtce.toolkit.XTCESpaceSystem;

/**
 *
 * @author David Overeem
 *
 */

public class XTCEViewerAliasSet extends javax.swing.JPanel {

    /**
     * Creates new form XTCEViewerAliasSet
     */
    public XTCEViewerAliasSet() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        aliasListScrollPane = new javax.swing.JScrollPane();
        addAliasButton = new javax.swing.JButton();
        aliasListLabel = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/xtce/toolkit/MessagesBundle"); // NOI18N
        addAliasButton.setText(bundle.getString("tab_ssdetail_addalias_text")); // NOI18N
        addAliasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAliasButtonActionPerformed(evt);
            }
        });

        aliasListLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        aliasListLabel.setText(bundle.getString("tab_ssdetail_aliaslist_text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aliasListScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 179, Short.MAX_VALUE)
                        .addComponent(addAliasButton)
                        .addGap(0, 180, Short.MAX_VALUE))
                    .addComponent(aliasListLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(aliasListLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aliasListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addAliasButton)
                .addGap(4, 4, 4))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addAliasButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAliasButtonActionPerformed
        addAlias( "NS", "New_Alias" );
    }//GEN-LAST:event_addAliasButtonActionPerformed

    public void setSpaceSystem( XTCESpaceSystem spaceSystem ) {

        spaceSystem_ = spaceSystem;
        aliasItemText_.clear();
        nsItemText_.clear();

        AliasSetType ast = spaceSystem.getReference().getAliasSet();
        if ( ast != null ) {
            List<Alias> aliases = ast.getAlias();
            for ( Alias alias : aliases ) {
                aliasItemText_.add( alias.getAlias() );
                nsItemText_.add( alias.getNameSpace() );
            }
        }

        makeContentPanel( nsItemText_, aliasItemText_ );

    }

    public void setEditable( boolean editEnabled ) {
        addAliasButton.setEnabled( editEnabled );
        JPanel notePanel = (JPanel)aliasListScrollPane.getViewport().getView();
        if ( notePanel == null ) {
            return;
        }
        Component[] components = notePanel.getComponents();
        for ( Component component : components ) {
            if ( component.getClass() == XTCEViewerAlias.class ) {
                ((XTCEViewerAlias)component).setEditable( editEnabled );
            }
        }
    }

    public void removeAlias( int idx ) {
        aliasItemText_.remove( idx );
        nsItemText_.remove( idx );
        makeContentPanel( nsItemText_, aliasItemText_ );
        updateDocument();
    }
    
    public void addAlias( String namespaceText, String aliasText ) {
        aliasItemText_.add( aliasText );
        nsItemText_.add( namespaceText );
        makeContentPanel( nsItemText_, aliasItemText_ );
        updateDocument();
    }

    public void editAlias( String namespaceText, String aliasText, int idx ) {
        aliasItemText_.set( idx, aliasText );
        nsItemText_.set( idx, namespaceText );
        updateDocument();
    }

    private void makeContentPanel( List<String> nsList, List<String> aliasList ) {

        JPanel notePanel = new JPanel();
        notePanel.setLayout( new BoxLayout( notePanel, BoxLayout.Y_AXIS ) );

        int idx = 0;

        for ( int iii = 0; iii < nsList.size(); ++iii ) {
            XTCEViewerAlias noteEntry = new XTCEViewerAlias( this,
                                                             nsList.get( iii ),
                                                             aliasList.get( iii ),
                                                             iii );
            notePanel.add( noteEntry );
        }

        aliasListScrollPane.setViewportView( notePanel );
        aliasListScrollPane.revalidate();
        aliasListScrollPane.repaint();

    }

    private void updateDocument() {

        // first shortcut if there are no aliases
        AliasSetType ast = spaceSystem_.getReference().getAliasSet();
        if ( aliasItemText_.isEmpty() == true ) {
            if ( ast != null ) {
                spaceSystem_.getReference().setAliasSet( null );
            }
            return;
        }

        // make sure we have an AliasSet element since have content for it
        if ( ast == null ) {
            ast = new AliasSetType();
            spaceSystem_.getReference().setAliasSet( ast );
        }

        // first clear the NoteSet element if there are no notes
        List<Alias> aliases = ast.getAlias();
        aliases.clear();
        for ( int iii = 0; iii < nsItemText_.size(); ++iii ) {
            Alias aliasItem = new Alias();
            aliasItem.setNameSpace( nsItemText_.get( iii ) );
            aliasItem.setAlias( aliasItemText_.get( iii ) );
            aliases.add( aliasItem );
        }

    }

    // Private Data Members

    private XTCESpaceSystem spaceSystem_ = null;

    private final ArrayList<String> aliasItemText_ = new ArrayList<>();
    private final ArrayList<String> nsItemText_    = new ArrayList<>();


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAliasButton;
    private javax.swing.JLabel aliasListLabel;
    private javax.swing.JScrollPane aliasListScrollPane;
    // End of variables declaration//GEN-END:variables
}
